package com.wzt.greenhousemobile;

import java.util.ArrayList;
import com.wzt.greenhouse.model.ControlInfo;
import com.wzt.greenhouse.model.GreenhouseControl;

import android.os.Handler;
import android.os.Message;

public class HardwareManager {
	static Boolean init = false;
	static Handler ctrlHandler = null;
	static String mainNo=null,subNo=null;
	static ArrayList<String> cmdStrings = new ArrayList<String>();
	static int activeTimer = 0;
	static Boolean startActiveTimer = false;
	static private int gotoTimeSpan = 30;
	private static ArrayList<ControlInfo> animationCtrls = new ArrayList<ControlInfo>();
	static int timeSpan = 2000;
	static String activityType = "sensorbox";
	private static boolean isRun = true;

	static public void setActiveTimer()
	{
		activeTimer = 0;
	}
	
	static public void startActriveTimer()
	{
		startActiveTimer = true;
		activeTimer = 0;
	}
	
	static public void endActiveTimer()
	{
		startActiveTimer = false;
		activeTimer = 0;
	}
	private static Runnable drawBackupRunnable = new Runnable(){
		@Override
		public void run(){
			while(isRun)
			{
				try {
					Thread.sleep(500);
					if(startActiveTimer)
					{
						activeTimer += 500;
					}
					Message localMessage = new Message();
					localMessage.obj="(PLAY#ANIMATION)";
					if(ctrlHandler!=null)
						ctrlHandler.sendMessage(localMessage);	
					
					if(activeTimer > gotoTimeSpan*1000)
					{
						localMessage = new Message();
						localMessage.obj="(GOTO_INFOVIEW)";
						if(ctrlHandler!=null)
							ctrlHandler.sendMessage(localMessage);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
				
			}
		}
	};
	public static int checkTimer = 120; 
	public static int controlStatetimer = 6;
	private static Runnable checkMonitorStateRunnable = new Runnable(){
		@Override
		public void run(){
			int span = 0;
			while(isRun)
			{
				try {
					Thread.sleep(1000);
					span++;
					if(HardwareManager.activityType.equals("sensorbox"))
					{
						if(span < checkTimer)
							continue;
						if(DPWebService.checkMonitorState())
						{
							if(ctrlHandler != null)
							{
								Message localMessage = new Message();
								localMessage.obj="(CheckState)";
								ctrlHandler.sendMessage(localMessage);	
							}
						}
						span = 0;
					}
					if(HardwareManager.activityType.equals("controller"))
					{
						if(span < controlStatetimer)
							continue;
						if(DPWebService.checkControllerState())
						{
							if(DPWebService.changeStateCtrlList.size() > 0)
							{
								Message localMessage = new Message();
								localMessage.obj="(CheckState)";
								if(ctrlHandler!=null)
									ctrlHandler.sendMessage(localMessage);
							}
						}
						span = 0;
						
					}
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
				
			}
		}
	};	
	public HardwareManager() {

	}

    public static void destroy()
    {
    	init = false;
    	isRun = false;
    	ctrlHandler = null;
    }	
	public static void setTimeSpan(int time)
	{
		timeSpan = time;
	}
	
	public static Handler getCtrlHandler() {
		return ctrlHandler;
	}
	
	public static void setCtrlHandler(Handler ctrlHandler) {
		HardwareManager.ctrlHandler = ctrlHandler;
		CourseAdapter.objMap.clear();
		animationCtrls.clear();
	}
	public static Boolean getInit() {
		return init;
	}
	
	public static void InitHardware()
	{
    	new Thread(drawBackupRunnable).start();
    	init = true;
	}

	public static void startCheckTimer()
	{
		isRun = true;
		new Thread(checkMonitorStateRunnable).start();
	}
    
    public static void setMainNo(String mainNo) {
		HardwareManager.mainNo = mainNo;
	}
    
    public static void setSubNo(String subNo) {
		HardwareManager.subNo = subNo;
	}
    
    public static void sendCmd(String cmd)
    {
    	if(cmd == null)
    		return;
    	if (cmd == "") 
    		return;
    	cmdStrings.add(cmd);
    }
    
	public static void AnimationRemove(ControlInfo controlInfo) {
		animationCtrls.remove(controlInfo);
	}

	public static int AnimationsCount() {
		return animationCtrls.size();
	}

	public static ControlInfo AnimationsIndex(int i) {
		// TODO Auto-generated method stub
		return animationCtrls.get(i);
	}

	public static void AnimationsAdd(ControlInfo controlInfo) {
		if(animationCtrls.contains(controlInfo))
			return;
		animationCtrls.add(controlInfo);
	}
    
	public static void uncode(String type,String reqContent,GreenhouseControl ghControl)
	{

	}
}
